package com.laowang.framework.listener;

import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.StringUtils;
import com.laowang.project.system.domain.*;
import com.laowang.project.system.mapper.MyCyhActivitySignMapper;
import com.laowang.project.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.Contended;

import java.util.Date;
import java.util.Objects;

/**
 * @Auther laowang
 * @Date 2024/4/10
 * @Description
 */

@Component
public class UtilMoney {

    @Autowired
    private WxOrderService wxOrderService;

    @Autowired
    private MyCyhActivitySignService myCyhActivitySignService;

    @Autowired
    private MyCyhActivitySignMapper myCyhActivitySignMapper;

    @Autowired
    private MyCyhActivityService myCyhActivityService;

    @Autowired
    private MyCyhTicketService myCyhTicketService;

    @Autowired
    private UserGoldLogService userGoldLogService;

    //@Transactional
    public  void  refound111(WxOrder wxOrder) {
        MyCyhActivity myCyhActivity = myCyhActivityService.selectMyCyhActivityById(Integer.valueOf(wxOrder.getRemark()));
        if (myCyhActivity !=null && !myCyhActivity.getCate().equals("3")){
            if (new Date().after(myCyhActivity.getAtimeStart())) {

                throw new CustomException("活动已开始暂不退款");
            }
        }

        wxOrder.setStatus(2);
        wxOrderService.updateWxOrder(wxOrder);

        //关联到活动保名的id
        String transactionId = wxOrder.getTransactionId();
        if (StringUtils.isNotEmpty(transactionId)){
            MyCyhActivitySign myCyhActivitySign = myCyhActivitySignService.selectMyCyhActivitySignById(Integer.valueOf(transactionId));
            if (myCyhActivitySign !=null){
                if (myCyhActivitySign.getIsPay().equals(1)){
                    myCyhActivitySign.setIsPay(2);//已退款
                    myCyhActivitySignMapper.updateMyCyhActivitySign(myCyhActivitySign);

                    //更新报名的人
                    Integer leave = myCyhActivity.getLeave();
                    Integer nums = myCyhActivitySign.getNums();
                    leave -= nums;
                    myCyhActivity.setLeave(leave);
                    myCyhActivityService.updateMyCyhActivity(myCyhActivity);

                    Integer ticketId = myCyhActivitySign.getTicketId();
                    if (!Objects.isNull(ticketId)){
                        MyCyhTicket myCyhTicket = myCyhTicketService.selectMyCyhTicketById(ticketId);
                        if (  myCyhTicket != null){

                            Integer numbers = myCyhTicket.getNumbers();
                            Integer nums1 = myCyhActivitySign.getNums();
                            numbers -=nums1;
                            myCyhTicket.setNumbers(numbers);
                            myCyhTicketService.updateMyCyhTicket(myCyhTicket);
                        }


                    }

                    UserGoldLog userGoldLog =new UserGoldLog();
                    userGoldLog.setSignId(myCyhActivitySign.getId());
                    UserGoldLog goldLog = userGoldLogService.selectUserGoldLogBySign(myCyhActivitySign.getId());
                    if (goldLog !=null){
                        goldLog.setStatus(3);//返回到系统
                        userGoldLogService.updateUserGoldLog(goldLog);
                    }

                }
            }
        }
    }
}
